function successCallback(result) {
	console.log("It succeeded with ", result);
}

function failureCallback(error) {
	console.log("It failed with ", error);
}

function loadData(successCallback, failureCallback) {
	var d = new Date();
	var seconds = d.getSeconds();
	if(seconds % 2 === 0) {
		successCallback({
			status: 'success',
			data: {
				seconds: seconds,
				description: 'the seconds is even'
			}
		});
	} else {
		failureCallback({
			status: 'failure',
			data: {
				seconds: seconds,
				description: 'the seconds is odd'
			}
		});
	}
}

var delay2Second = () => {
	console.log("2 seconds passed");
	throw new Error('is a custom error :-)')
};
var logError = e => console.error('error:', e.message);

const wait = time => new Promise(resolve => setTimeout(resolve, time));
const delay = wait;

let applyAsync = (acc, val) => acc.then(val);
let composeAsync = (...funcs) => x => funcs.reduce(applyAsync, Promise.resolve(x));

let counter1 = 100;
let counter2 = 200;

let func1 = () => console.log("this is func1--" + counter1++);
let func2 = () => console.log("this is func2--" + counter2++);
let func3 = () => console.log("this is func3--");
let asyncFunc1 = async() => {
	for(let f of [func1, func2]) {
		await f();
	}
}
let asyncFunc2 = asyncFunc1;

let wait2000ms = new Promise(function(resolve, reject) {
	setTimeout(resolve, 2000, 'icedog');
});

var icedog = {
	get: (url) => {
		return new Promise((resolve, reject) => {
			const xhr = new XMLHttpRequest();
			xhr.open("GET", url, true);
			xhr.onload = () => resolve(xhr.responseText);
			xhr.onerror = () => reject(xhr.statusText);
			xhr.send();
		})
	}
}

//-----------------------------
window.onload = main;

function main() {
	console.log('main running...');
	var box1 = document.getElementById("box1");
	var line = 1;
	const writeResult = (data) => {
		var p = document.createElement('p');
		p.innerHTML = '<span style="color:green;font-size:40px;">' + line + ':</span>';
		p.innerHTML += data;
		line += 1;
		box1.appendChild(p);
	}
	//	setTimeout(delay2Second, 2000);
	//	wait(2000).then(delay2Second).catch(logError);
	//	[func1, func2].reduce((p, f) => p.then(f), Promise.resolve());
	//	Promise.resolve().then(func1).then(func2);
	//	applyAsync(Promise.resolve(), func1);
	//	composeAsync(func1, func2);

	//	let transformData = composeAsync(func1, asyncFunc1, asyncFunc2, func2);
	//	transformData();

	//	wait().then(() => console.log(4));
	//	Promise.resolve().then(() => console.log(2)).then(() => console.log(3));
	//	console.log(1); // 1, 2, 3, 4

	//	wait2000ms.then((...params)=>console.log('2 seconds passed',params));

	//	icedog.get("data1.json").then(writeResult).catch(writeResult);
	//	icedog.get("data2.json").then(writeResult).catch(writeResult);
	
}